<template>
  <div class="mod-sp__spmatch">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.matchName" placeholder="赛事名称" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <ren-select v-model="dataForm.season" dict-type="season" placeholder="赛季年度"></ren-select>
      </el-form-item>
      <el-form-item label="地区" prop="province">
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.province" @change="selectCity">
          <el-option value="" label="全国">全国</el-option>
          <el-option v-for="itemO in area.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
        ~
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.city">
          <el-option value="" label="全部">全部</el-option>
          <el-option v-for="itemO in areaCity.item" :key="itemO.code" :label="itemO.name" :value="itemO.code"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatch:export')" type="info" @click="exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('sp:spmatch:save')" type="primary" @click="uploadPormotion()">导入</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column width="80" prop="id" label="赛事ID" header-align="center" align="center"></el-table-column>
      <el-table-column width="200" prop="matchName" label="赛事名称" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="showLimitList(scope.row)">{{ scope.row.matchName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column width="120" prop="matchLevel" label="赛事等级" header-align="center" align="center">
        <template v-slot="scope">
          {{ getDictLabel("matchLevel", scope.row.matchLevel) }}
        </template>
      </el-table-column>
      <el-table-column width="200" prop="matchParentName" label="母赛事" header-align="center" align="center"></el-table-column>
      <el-table-column prop="province" label="省" header-align="center" align="center">
        <template v-slot="scope">
          {{ scope.row.province === "-1" ? "全国" : scope.row.provinceName }}
        </template>
      </el-table-column>
      <el-table-column prop="city" label="市" header-align="center" align="center">
        <template v-slot="scope">
          {{ scope.row.city === "-1" ? "全部" : scope.row.city }}
        </template>
      </el-table-column>
      <el-table-column prop="isOpen" label="跨地区" header-align="center" align="center">
        <template v-slot="scope">
          {{ getDictLabel("isOpen", scope.row.isOpen) }}
        </template>
      </el-table-column>
      <el-table-column width="120" prop="season" label="赛季年度" header-align="center" align="center">
        <template v-slot="scope">
          {{ getDictLabel("season", scope.row.season) }}
        </template>
      </el-table-column>
      <el-table-column width="190" prop="beginDate" label="比赛时间起" header-align="center" align="center">
        <template v-slot="scope"> {{ scope.row.beginDate }} ~ {{ scope.row.endDate }} </template>
      </el-table-column>
      <el-table-column width="190" prop="beginSignup" label="报名时间起" header-align="center" align="center">
        <template v-slot="scope"> {{ scope.row.beginSignup }} ~ {{ scope.row.endSignup }} </template>
      </el-table-column>
      <el-table-column width="120" prop="adminName" label="管理员" header-align="center" align="center"></el-table-column>
      <el-table-column width="120" prop="status" label="赛事状态" header-align="center" align="center">
        <template v-slot="scope">
          {{ getDictLabel("matchStatus", scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="hasPermission('sp:spmatch:setting')" type="text" size="small" @click="showLimitList(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 字典类型数据 -->
    <el-drawer :title="focusDictTypeTitle" v-model="dictTypeListVisible" :size="800" close-on-press-escape="false" custom-class="rr-drawer"><DictTypeList :dictTypeId="focusDictTypeId"></DictTypeList></el-drawer>
    <!--查看详情-->
    <detail v-if="detailVisible" ref="matchRef" @refreshDataList="getDataList"></detail>
    <!--地区配置-->
    <el-drawer :title="matchAreaTitle" v-model="matchAreaVisible" :size="800" close-on-press-escape="false" custom-class="rr-drawer">
      <MatchArea :dictTypeId="matchAreaId" :areaProvinceCode="areaProvinceCode"></MatchArea>
    </el-drawer>
    <!-- 字典类型数据 -->
    <el-drawer :title="dictLimitTypeTitle" v-model="dictLimitListVisible" :size="800" close-on-press-escape="false" custom-class="rr-drawer"><DictTypeList2 :dictTypeId="dictLimitTypeId"></DictTypeList2></el-drawer>
    <upload v-if="uploadVisible2" ref="upload2" uploadurl="/sp/spmatcharealimit/import" downloadurl="/sp/spmatcharealimit/exportError1" @refreshDataList="getDataList"></upload>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./spmatch-add-or-update.vue";
import DictTypeList from "./spmatchitem.vue";
import DictTypeList2 from "./spmatchitem6.vue";
import MatchArea from "./spmatcharea.vue";
import { IObject } from "@/types/interface";
import baseService from "@/service/baseService";
import Detail from "@/views/sp/spmatch-detail.vue";
import Upload from "./sp-limit.vue";
import app from "@/constants/app";
import {getToken} from "@/utils/cache";

export default defineComponent({
  components: {
    Upload,
    AddOrUpdate,
    DictTypeList,
    DictTypeList2,
    MatchArea,
    Detail
  },
  setup() {
    const state = reactive({
      getDataListURL: "/sp/spmatch/page",
      getDataListIsPage: true,
      exportURL: "/sp/spmatcharealimit/exportLimit",
      deleteURL: "/sp/spmatch",
      deleteIsBatch: true,
      dataForm: {
        matchName: "",
        matchLevel: "2",
        province: "",
        city: "",
        season: ""
      },
      dictTypeListVisible: false,
      focusDictTypeId: "",
      focusDictTypeTitle: "",
      matchAreaVisible: false,
      matchAreaId: "",
      areaProvinceCode: "",
      matchAreaTitle: "",
      area: { code: "0", item: [{ code: "0", name: "请选择" }] },
      areaCity: { code: "0", item: [{ code: "0", name: "请选择" }] },
      detailVisible: false,
      dictLimitListVisible: false,
      dictLimitTypeId: "",
      dictLimitTypeTitle: "",
      uploadVisible2: false
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  created() {
    this.areaList(this.area);
  },
  methods: {
    // 在组件中使用
    downloadExcel(){
      window.open(`${app.api}/sp/spmatcharealimit/exportLimit2?token=${getToken()}`);
    },

    detailHandle(id?: string) {
      this.detailVisible = true;
      baseService.get("/sp/spmatch/" + id).then((e: any) => {
        this.$nextTick(() => {
          this.$refs.matchRef.dataForm = e.data;
          this.$refs.matchRef.init();
        });
      });
    },
    // 获取省份
    areaList(target: any) {
      baseService.get("/front/fast/sys/areaDict/list?pid=" + target.code).then((e: any) => {
        target.item = e.data;
      });
    },
    // 选择城市
    selectCity() {
      if (this.dataForm.province) {
        this.areaCity.code = this.dataForm.province;
        this.areaList(this.areaCity);
      } else {
        this.dataForm.city = "";
        this.areaCity.item = [];
      }
    },
    showTypeList(row: IObject) {
      this.dictTypeListVisible = true;
      this.focusDictTypeId = row.id;
      this.focusDictTypeTitle = `${this.$route.meta.title}` + "-" + row.matchName;
    },
    showLimitList(row: IObject) {
      this.dictLimitListVisible = true;
      this.dictLimitTypeId = row.id;
      this.dictLimitTypeTitle = `${this.$route.meta.title}` + "-" + row.matchName;
    },
    settingHandle(row: IObject) {
      this.matchAreaVisible = true;
      this.matchAreaId = row.id;
      this.areaProvinceCode = row.province;
      this.matchAreaTitle = `${this.$route.meta.title}` + "-" + row.matchName + "-" + row.id;
    },
    uploadPormotion() {
      this.uploadVisible2 = true;
      this.$nextTick(() => {
        this.$refs.upload2.init();
      });
    }
  }
});
</script>
